package com.fxd.shopping.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxd.shopping.product.dto.BrandByCatIdDTO;
import com.fxd.model.product.BrandEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fxd.model.product.CategoryBrandRelationEntity;
import com.fxd.shopping.product.service.CategoryBrandRelationService;
import com.fxd.core.util.PageUtils;
import com.fxd.core.util.RR;


/**
 * 品牌分类关联
 *
 * @author fuxdong
 * @email 2016335176@qqcom
 * @date 2021-07-17 10:30:51
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public RR list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return RR.ok().put("page", page);
    }

    /**
     * 获取某个分类下所有品牌信息
     *
     * @param catId
     * @return
     */
    @GetMapping("/brands/list")
    public RR relationBrandList(@RequestParam(value = "catId", required = true) String catId) {

        List<BrandEntity> brandEntities = categoryBrandRelationService.getBrandByCatId(catId);

        List<BrandByCatIdDTO> collect = brandEntities.stream().map((brand) -> {
            BrandByCatIdDTO brandByCatIdDTO = new BrandByCatIdDTO();
            brandByCatIdDTO.setBrandId(String.valueOf(brand.getBrandId()));
            brandByCatIdDTO.setBrandName(brand.getName());
            return brandByCatIdDTO;
        }).collect(Collectors.toList());
        return RR.ok().put("data", collect);
    }

    /**
     * 列表
     */
    @GetMapping("/catelog/list")
    public RR catelogList(@RequestParam Long brandId) {
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));

        return RR.ok().put("data", data);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public RR info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return RR.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RR save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return RR.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RR update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return RR.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RR delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return RR.ok();
    }

}
